<?php
class Admin_entreprise extends Admin_Controller{
	public function __construct(){
		parent :: __construct();
		$this->load->model("Company_m");
		$this->_data['sidebar'] = "company";
		
	}
	
	public function index(){
		$this->list_entreprise();	
	}
	
	//--list companies--//
	public function list_entreprise(){
		is_admin();
		$this->_data['page_title'] = "Entreprise";
		$perpage = $this->uri->segment(3);
		$offset = $this->uri->segment(4);
		if($perpage == ""){
			$perpage = 10; 
		}
		if($offset == ""){
			$offset = "";
		}
		$config['base_url'] = base_url()."index.php/admin_entreprise/list_entreprise/".$perpage."/";
		$config['per_page'] = $perpage;
		$config['uri_segment'] = 4;
		$config['total_rows'] = $this->Company_m->_count_companies();
		if($offset > 0 && $offset == $config['total_rows']){
			$offset = $offset - $perpage;
		}
		$this->pagination->initialize($config);
    	$this->_data['pagination'] = $this->pagination->create_links();
    	$this->_data['total'] = $config['total_rows'];
    	$this->_data['perpage'] = $perpage;
    	$this->_data['offset'] = $offset;
		$query = $this->Company_m->_get_companies($perpage, $offset);
		$this->_data['companies_data'] = $query;
		$this->display_admin("admin/company/list_companies");	
	}
	
	//-- add new company --//
	public function ajouter_entreprise(){
		is_admin();
		$this->_data['page_title'] = "Ajouter Entreprise";
		
		$config = array(
							array("field" => "name",
							      "label" => "nom",
								  "rules" => "required|trim|xss_clean|callback__check_exist_company_name"
							      ),
							
							array("field" => "description",
							      "label" => lang("description"),
								  "rules" => "trim|xss_clean"
							      )
		
						);
		$this->form_validation->set_rules($config);//set validation rules
		
		if($this->input->post("submit")){
			if($this->form_validation->run()){
				$data = array("name" => $this->input->post("name"), "description" => $this->input->post("description"));
				$q_add_company = $this->Company_m->add_company($data);
				if($q_add_company){
						$this->session->set_flashdata("error", lang("add"). " ".lang("success"));
						redirect("admin_entreprise");	
					}else{
						$this->session->set_flashdata("error", lang("add"). " ".lang("fail"));
						redirect("admin_entreprise");	
					}		
			}else{
				$this->display_admin("admin/company/add_company");	
			}	
		}else{
			$this->display_admin("admin/company/add_company");	
		}
	
	}
	
	//-- edit company information --//
	function modifier_entreprise($id=null){
		is_admin();
		$this->_data['page_title'] = "Modifier Entreprise";
		if(!isset($id) || $id == NULL){
			redirect("admin_entreprise");
		}elseif($this->Company_m->_get_company_infor($id)->num_rows()<1){
			redirect("admin_entreprise");
		}
				
		//load company infor
		$company_infor = $this->Company_m->_get_company_infor($id)->row();
		$this->_data['id'] = $company_infor->id;
		$this->_data['name'] = $company_infor->name;
		$this->_data['description'] = $company_infor->description;
		
		//send id to callback
		$field = array($id);
		
		$config = array(
							array("field" => "name",
							      "label" => "nom",
								  "rules" => "required|trim|xss_clean|callback__check_new_name_edit[$field[0]]]"
							      ),
							
							array("field" => "description",
							      "label" => lang("description"),
								  "rules" => "trim|xss_clean"
							      )
		
						);
		$this->form_validation->set_rules($config);//set validation rules
		
		if($this->input->post("submit")){
			if($this->form_validation->run()){
				$data = array("name" => $this->input->post("name"), "description" => $this->input->post("description"));
				$q_edit_company = $this->Company_m->edit_company($id, $data); 
				if($q_edit_company){
						$this->session->set_flashdata("error", lang("edit_profile"). " ".lang("success"));
						redirect("admin_entreprise");	
					}else{
						$this->session->set_flashdata("error", lang("edit_profile"). " ".lang("fail"));
						redirect("admin_entreprise");	
					}		
			}else{
				$this->display_admin("admin/company/edit_company");	
			}	
		}else{
			$this->display_admin("admin/company/edit_company");	
		}
			
		
	}
	
	//-- delete company--//
	function supprimer($id){
		$q_delete_company = $this->Company_m->delete_company($id);
		if($q_delete_company){
			$this->session->set_flashdata("error", lang("delete"). " ".lang("success"));
			redirect("admin_entreprise");		
		}else{
			$this->session->set_flashdata("error", lang("delete"). " ".lang("fail"));
			redirect("admin_entreprise");	
		}
	}
	
	
	//--check new name when edit company was exist
	function _check_new_name_edit($str, $id){
		
		$query = $this->Company_m->_get_name_except_current_name($id, $str);
		if($query->num_rows()>0){
			$this->form_validation->set_message("_check_new_name_edit", "L'entreprise nom existe déjà dans le système");
			return FALSE;
		}else{
			return TRUE;
		}
	}
	
	//--check exist company name--//
	function _check_exist_company_name($str){
		if($this->Company_m->_get_company_name($str)){
			if($this->Company_m->_get_company_name($str)->num_rows()>0){
				$this->form_validation->set_message("_check_exist_company_name", "L'entreprise nom existe déjà dans le système");
				return FALSE;
			}else{
				return TRUE;
			}
		}
	}

	
}